import * as types from '../mutation-types'
import api from 'api'
// import _ from 'lodash'
// initial state
const state = {
  devices: []
}

const actions = {

  // 三防（数据）
  updatePhoneStatistics ({ commit }) {
    api.phone.getPhones().then((res) => {
      let arr = []
      // 做一遍过滤定位为0,0的数据给排除
      res.data.phones.forEach((item, index) => {
        if (item.location.lat <= 90 && item.location.lat >= -90 && item.location.lng > -180 && item.location.lng < 180 && item.location.lng !== 0 && item.location.lat !== 0) {
          arr.push(item)
        }
      })
      commit(types.UPDATE_PHONE_STATISTICS, arr)
    }).catch((res) => {
      console.warn('保利获取三防设备接口报错', res)
    })
  }
}

const getters = {
  phonesDevices (state) {
    return state.devices
  },
  pads (state) {
    return _.filter(state.devices, (item) => {
      return item.type - 0 === 1
    })
  },
  phones (state) {
    return _.filter(state.devices, (item) => {
      return item.type - 0 === 2
    })
  },
  online (state) {
    return _.filter(state.devices, (item) => {
      return item.status - 0 === 1
    })
  },
  offline (state) {
    return _.filter(state.devices, (item) => {
      return item.status - 0 === 0
    })
  }
}

// mutations
const mutations = {
  [types.UPDATE_PHONE_STATISTICS] (state, arr) {
    if (!arr) return
    if (!arr.length) return
    state.devices = arr
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
